package com.trimps.wifi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trimps.wifi.common.Constants;
import com.trimps.wifi.common.Paginator;
import com.trimps.wifi.pojo.Alarm;
import com.trimps.wifi.pojo.BlacklistInfoData;
import com.trimps.wifi.pojo.FlowData;
import com.trimps.wifi.service.IAlarmService;

@Controller
@RequestMapping(value = "alarm/")
public class AlarmController extends BaseController {
    private static final Logger logger = Logger.getLogger(AlarmController.class);

    @Autowired
    private IAlarmService alarmService;

    @RequestMapping(value = "list")
    public String showAlarmListPage() {
        return "alarm/list";
    }
    
	@RequestMapping(value = "listInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> alarmlist(@RequestParam(value="curPage", required = false) String cp, @RequestParam(value="totalPage", required = false) String tp,
                    @RequestParam(value="keywords", required = false) String keywords) {
	    // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        int curPage = Constants.DEFAULT_FIRST_PAGE, totalCount, totalPage;
        List<Alarm> lst = new ArrayList<Alarm>();
        // if ("".equals(tp)) {
        totalCount = alarmService.getAlarmCount(keywords);
        if (totalCount == 0) {
            totalPage = 0;
        } else {
            // 计算totalpage
            totalPage = (int) Math.ceil((double) totalCount / (double) Constants.RECORD_PER_PAGE);
        }

        if (totalPage != 0) {
            if (!("".equals(cp))) {
                curPage = Integer.parseInt(cp);
            }
            lst = alarmService.getAllAlarms(keywords, curPage, Constants.RECORD_PER_PAGE);
        }

        map.put("totalPage", totalPage);
        map.put("currentPage", curPage);
        map.put("listInfo", lst);
        map.put("totalCount", totalCount);
        return map;
    }
	
	@RequestMapping(value = "alarmList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getAlarmListByKeyword(
                    @RequestParam(value="keywords", required = false) String keywords,
                    @RequestParam(value="deviceNo", required = false) String deviceNo,
                    @RequestParam(value="startTime", required = false) String startTime,
                    @RequestParam(value="endTime", required = false) String endTime)  {
	    // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        int totalCount;
        List<Alarm> lst = new ArrayList<Alarm>();
        totalCount = alarmService.getAlarmListCountByKeyword(keywords, deviceNo, startTime, endTime);
        lst = alarmService.getAlarmListByKeyword(keywords, deviceNo, startTime, endTime);
        
        map.put("listInfo", lst);
        map.put("totalCount", totalCount);
        return map;
    }
}
